//
//  SmartInstallListViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/17.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
import CRRefresh

class SmartInstallListViewController: BaseViewController {

    @IBOutlet var installNumLabel: UILabel!
    
    @IBOutlet var installListTableview: UITableView!
    
    let installCell = "installCell"
    var installs :[InstallModel]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        page = 1
        requstInstalledList(page: page!)
        configView()
    }
    
    func configView() -> Void {
        installListTableview.delegate = self
        installListTableview.dataSource = self
        installListTableview.separatorStyle = .none
        installListTableview.showsVerticalScrollIndicator = false
        installListTableview.backgroundColor = BaseColor.BackGroundColor
        installListTableview.register(UINib(nibName: "SmartInstallListTableViewCell", bundle: nil), forCellReuseIdentifier: installCell)
        installListTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
            page = 1
            self?.requstInstalledList(page: page!)
        }
        installListTableview.cr.addFootRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol)  { [weak self] in
            page! += 1
            self?.requstInstalledList(page: page!)
        }
    }
    
    //网络请求
    func requstInstalledList(page:Int) -> Void {
        
        Alamofire.request(smartControlInstallList, method:.post, parameters: ["page": page,"sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
            
                self.installListTableview?.cr.endHeaderRefresh()
                self.installListTableview?.cr.endLoadingMore()
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<InstallModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        self.installNumLabel.text = "共安装" + (basemodel.data?.total)! + "台"
                        if page == 1 {
                            self.installs?.removeAll()
                            self.installs = basemodel.data?.data
                        }else {
                            if ((basemodel.data?.data?.count)! > 0) {
                                self.installs! += (basemodel.data?.data)!
                            }else {
                                return
                            }
                        }
                    self.installListTableview.reloadData()
                        print("数据成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
                
                
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension SmartInstallListViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.installs?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
            let cell = tableView.dequeueReusableCell(withIdentifier: installCell, for: indexPath) as! SmartInstallListTableViewCell
            cell.selectionStyle = .none
            let model = self.installs![indexPath.item]
            cell.installTimeLabel.text = "安装时间：" + model.install_time
            cell.deviceNumLabel.text = "设备号：" + model.device_num
            cell.phoneLabel.text = "电话号码：" + model.phone
            cell.remarksLabel.text = "备注：" + model.remarks
            return cell
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return ScaleHeight(100)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    //    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //        return 10
    //    }
    
}


